<?php
// +--------------------------------------------------------------------------
// | ZAIYOUDAO [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2016 载攸道 版权所有 <TEL:17090888854>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * 用户管理
 */
namespace Research\Controller;
use Common\Controller\AdminController;

/**
 * 用户管理控制器
 */
class DataController extends AdminController{

	/*
	 * 用户管理
	 */
    public function Detail(){
		
		action_log('admin_user_manage');//行为记录
		
		common_public()->assign_page('调研管理');
		
		isFormAuth(I('param.id'),11);
		
		
		$m_research_form = M('research_form');
		$m_research_info = M('research_info');
		
		$map['form_id'] = I('param.id');
		$map['pid'] = 0;
		$map['status'] = 1;
		
		
		$begin_time = strtotime(I('param.begin_time'));
		$end_time = strtotime(I('param.end_time'));
		if($begin_time != null and $end_time != null){
			
			$map['time']  = array(array('egt',$begin_time),array('elt',$end_time),'and'); 
			
		}else{
			
			$begin_time=time();
			$end_time=time()+86400;
		}
		
		
		$this->assign('begin_time',$begin_time);
		$this->assign('end_time',$end_time);
		
		
		$list = $m_research_info->where($map)->order('time desc')->page($_GET['p'].',50')->select();
		$count      = $m_research_info->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$this->assign('pages',$show);
		$this->assign('id',I('param.id'));
		
		
		$form_map['form_id'] = I('param.id');
		$form_map['type'] = 2;
		$form_map['status'] = 1;
		
		$form_list = $m_research_form->where($form_map)->order('sort desc')->select();
		
		$keyword = I('param.keyword');
		
		
		
		foreach($list as $i=>$k){
			
			$form_info_map['pid'] = $list[$i]['id'];
			
			//填表信息
			$info = $m_research_info->where($form_info_map)->select();
			
			foreach($form_list as $ii=>$ik){
				
				if($ii==0){
					
					$infos = "<td>".getFormInfo($form_list[$ii]['id'],$info)."</td>";
				
					
				}else{
					
					$infos = $infos."<td>".getFormInfo($form_list[$ii]['id'],$info)."</td>";
				}
				
				
				
				
				
			}
			
			$infos = $infos."<td>"."<a class='btn btn-info btn-xs' href='/Research/Data/Edit/id/".$list[$i]['id'].".html'>修改</a>"."</td>";
			
			$list[$i]['info']  = $infos;
			
			if($keyword!=null){
				
				if(strpos($infos, $keyword)){
					
					
				}else{
					
					unset($list[$i]);
				}
				
			}
			
			
			
		}
	
	
		$this->assign('keyword',$keyword);
		$this->assign('form_list',$form_list);
		$this->assign('form_lists',$form_list);
		$this->assign('list',$list);
		
		
		$this->display();
	}
	
	/*
	 * 新增调研
	 */
    public function Edit(){
		
		common_public()->assign_page('编辑资料');
		
		$m_research_form = M('research_form');
		$m_research_info = M('research_info');
			
		if(IS_POST){
			
			$list = I();
			
			
			
			foreach($list as $i=>$k){
				
				
				$map['id'] = $i;
				$data['value'] = $k;
				
				
				$m_research_info->where($map)->save($data);
			}
			
			
				
				$this->success('编辑成功');
				
			
			
		}else{
			
			$form_info_map['pid'] = I('param.id');
			
			//填表信息
			$info = $m_research_info->where($form_info_map)->select();
			
			$this->assign('info',$info);
			$this->assign('id',$form_info_map['pid']);
			
			$this->display();
		}
		
	}
	
	/*
	 * 新增调研
	 */
    public function Create(){
		
		common_public()->assign_page('新增调研');
		
		$m_research_form = M('research_form');
			
		if(IS_POST){
			
			$data['name'] = I('param.name','','');
			$data['value'] = I('param.value','','');
			$data['type'] = 1;
			$data['form_id'] = 0;
			$data['uid'] = getUID();
			$data['ip'] = get_client_ip();
			$data['time'] = time();
			$data['status'] = 1;
			
			$result = $m_research_form->add($data);
			
			if($result){
				
				$url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/forms/'.$result;
				
				$maps['id'] = $result;
				$datas['isurl'] = get_url_from_isurl($url);
				$datas['qrcode'] = get_qr_code($datas['isurl']);
				
				$m_research_form->where($maps)->save($datas);
				
				
				$this->success('新增成功', U('Research/Form/Manage'));
				
			}else{
				
				$this->error('新增失败');
				
			}
			
		}else{
			
			$this->display();
		}
		
	}
	
	
	/*
	 * 编辑用户信息
	 */
	public function Status($id,$status=0){
		
		//判断是否是ajax请求
		if(IS_AJAX){
				
			$m_research_info = M('research_info');
		
			$id = $id?$id:I('param.id');
			$status = $status?$status:I('param.status');
			
			
			if($id){
				
			
				$id = explode(',',$id); 
				
				
				foreach($id as $i=>$k){
					
					$map['id'] = $id[$i];
					
				
					$rdata = $m_research_info->where($map)->find();
					
					isFormAuth($rdata['form_id'],12);
					
					
						
					$data['status'] = $status;
			
					$m_research_info->where($map)->save($data);
					
					
					
					
				}
				
				//返回信息
				$info = get_errcode(0);
			
			}else{
				
				$info = get_errcode(10000001);
			}
			
	
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);


	}
	
	
}